// packageC/Myshaky/enrollEnter/enrollEnter.js
import util from '../../../utils/util.js';
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nowTime: '',
    list: '',
    activity_id: '',
    guanz: '',

    exchange_code_id:"",//兑换码日志id
    set:{}

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id);
    this.setData({
      activity_id: options.id,
      exchange_code_id:options.exchange_code_id || ''
    });
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split(',');
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split('=');
          if (chil_arr[0] == "mid") {
            app._setMid(chil_arr[1]);
          }
          if (chil_arr[0] == "id") {
            this.setData({
              activity_id: chil_arr[1]
            });
          }
        }
      }
    }
    this.getrenshu();
    this.getAttention();
  },
  goshouye() {
    wx.reLaunch({
      url: '/packageC/Myshaky/activityHome/activityHome',
    });
  },
  goMy() {
    wx.navigateTo({
      url: '/packageC/Myshaky/Myindex/Myindex',
    });
  },
  // 获取是否有关注
  getAttention() {
    let urlStr = app.getNetAddresss("plugin.activity-apply.api.activity.isAttention");
    let that = this;
    app._getNetWork({
      url: urlStr,
      data: {
        activity_id: that.data.activity_id
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          if (!app._isTextEmpty(res.data)){
            that.setData({
              guanz: res.data.is_attention
            });
          }else{
            that.setData({
              guanz: 0
            });
          }
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  goGz(e) {
    console.log(wx.getStorageSync('yz_uid'));
    let urlStr = app.getNetAddresss("plugin.activity-apply.api.activity.attention");
    let that = this;
    app._getNetWork({
      url: urlStr,
      data: {
        activity_id: that.data.activity_id,
        member_id: wx.getStorageSync('yz_uid')
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          wx.showToast({
            title: res.msg,
            duration: 500,
            icon: 'none'
          });
          that.setData({
            guanz: !that.data.guanz
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  goBm(e) {
    if(this.data.is_auth == 1 && this.data.has_avatar != 1){
      // 会员头像，昵称没有设置先跳转到个人信息页面设置 perf#31514
      wx.navigateTo({
        url: '/packageA/member/new_userinfo/new_userinfo?plugin=activityEnroll',
      })
      return
    }
    if(this.data.count>=this.data.list.limit){
      wx.showToast({
        title: '报名人数已满',
        duration: 1500,
        icon: 'none'
      });
      return;
    }
    if(this.data.exchange_code_id){
      wx.navigateTo({
        url: '/packageC/Myshaky/enroll/enroll?id=' + e.currentTarget.id + "&fid=" + this.data.form_id + '&exchange_code_id='+this.data.exchange_code_id
      });
      return;

    }
    wx.navigateTo({
      url: '/packageC/Myshaky/enroll/enroll?id=' + e.currentTarget.id + "&fid=" + this.data.form_id 
    });
  },
  gobmqk(e) {
    console.log(e.currentTarget.dataset.id);
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageC/Myshaky/Registration/Registration?id=' + id,
    });
  },
  goMyind(e) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var value = wx.getStorageSync('yz_uid');
    var mid = '';
    if (value) {
      mid = value;
    }
    let json={path: "/packageC/Myshaky/enrollEnter/enrollEnter?id=" + this.data.activity_id + '&mid=' + mid};

    let {share_title:title1,share_thumb:thumb1,share_desc:desc1} = this.data.list.has_one_share;
    let {share_title:title2,share_thumb:thumb2,share_desc:desc2} = this.data.list.set;
    json.title = title1||title2||'';
    json.imageUrl = thumb1||thumb2||'';
    return json;
  },
  onShareTimeline:function() {
    var value = wx.getStorageSync('yz_uid');
    var mid = '';
    if (value) {
      mid = value;
    }
    let json={path: "/packageC/Myshaky/enrollEnter/enrollEnter?id=" + this.data.activity_id + '&mid=' + mid};

    let {share_title:title1,share_thumb:thumb1,share_desc:desc1} = this.data.list.has_one_share;
    let {share_title:title2,share_thumb:thumb2,share_desc:desc2} = this.data.list.set;
    json.title = title1||title2||'';
    json.imageUrl = thumb1||thumb2||'';
    return json;
  },
  
  goToAdress(){
    let latitude = this.data.latitude;
    let longitude = this.data.longitude;
    let _name = this.data.list.title;
    let point = app.BMapTransqqMap(parseFloat(longitude), parseFloat(latitude));
    wx.openLocation({
      latitude: point.lat,
      longitude: point.lng,
      scale: 18,
      name: _name
    });
  },
  goJs(e) {
    var kid = e.currentTarget.id;
    var eid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageC/Myshaky/lecturerDetail/lecturerDetail?id=' + kid + '&&eid=' + eid ,
    });
  },
  getrenshu() {
    let urlStr = app.getNetAddresss("plugin.activity-apply.api.activity.getDetails");
    let that = this;
    app._getNetWork({
      url: urlStr,
      data: {
        activity_id: that.data.activity_id
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          // console.log(res.data.data.length === 0)
          if (res.data.data.length === 0) {
            that.setData({
              iswu: true
            });
          } else {
            that.setData({
              iswu: false
            });
          }
          that.setData({
            datalist: res.data
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getList() {
    let urlStr = app.getNetAddresss("plugin.activity-apply.api.Activity.getActivity");
    let that = this;
    app._getNetWork({
      url: urlStr,
      data: {
        activity_id: that.data.activity_id
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          var minlis = res.data.registration_end_time;
          var minlisTime = util.formatTimeTwo(minlis, 'Y/M/D h:m:s');
          res.data.start_time = util.formatTimeTwo(res.data.start_time, 'Y/M/D h:m:s');
          res.data.end_time = util.formatTimeTwo(res.data.end_time, 'Y/M/D h:m:s');
          var timestamp = Date.parse(new Date());
          var timestamps = timestamp / 1000;
          if (res.data.cash <= "0.00") {
            that.setData({
              mf: true
            });
          } else {
            that.setData({
              mf: false
            });
          }
          if (app._isTextEmpty(res.data.has_many_pay_order)){
            that.setData({
              count:'0'
            });
          }else{
            that.setData({
              count: res.data.has_many_pay_order[0].count
            });
          }
          that.setData({
            list: res.data,
            nowTime: timestamps,
            minlisTime: minlisTime,
            form_id: res.data.form_id,
            longitude:res.data.longitude ? res.data.longitude : '',
            latitude:res.data.latitude ? res.data.latitude : '',
            set:res.data.set,
            is_auth : res.data.is_auth,
            has_avatar: res.data.has_avatar,
          });
          wx.setNavigationBarTitle({
            title: that.data.list.title
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
});
